package com.wlyy.his.service.pc.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wlyy.his.bean.user.pc.dto.PatientDto;
import com.wlyy.his.bean.user.pc.vo.PatientCertificateVo;
import com.wlyy.his.bean.user.pc.vo.PatientListVo;
import com.wlyy.his.bean.user.pc.vo.PatientVo;
import com.wlyy.his.common.domain.CertType;
import com.wlyy.his.common.exception.Asserts;
import com.wlyy.his.common.util.desensitization.DesensitizationUtils;
import com.wlyy.his.common.util.enumUtil.idcard.IDCardUtil;
import com.wlyy.his.common.util.theadLocal.UserUtils;
import com.wlyy.his.entity.pc.Patient;
import com.wlyy.his.entity.pc.PatientCertificate;
import com.wlyy.his.entity.registration.HisDoctorSchedule;
import com.wlyy.his.entity.ums.Certificate;
import com.wlyy.his.infrastructure.common.RequestUserInfo;
import com.wlyy.his.mapper.pc.PatientCertificateMapper;
import com.wlyy.his.mapper.pc.PatientMapper;
import com.wlyy.his.service.pc.IPatientCertificateService;
import com.wlyy.his.service.pc.IPatientService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Priority;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 就诊人 服务实现类
 * </p>
 *
 * @author ZhouYanhui
 * @since 2021-09-02
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements IPatientService {

    @Resource
    private PatientMapper patientMapper;
    @Resource
    private PatientCertificateMapper patientCertificateMapper;
    @Resource
    private IPatientCertificateService patientCertificateService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insert(PatientDto patientDto) {
        //根据证件信息判断是否添加过
        boolean exit = true;
        PatientCertificate pc = patientCertificateMapper.selectOne(Wrappers.lambdaQuery(PatientCertificate.class)
                .eq(PatientCertificate::getCertNo, patientDto.getCertNo()));
        if (null!=pc){
            Patient patient = patientMapper.selectOne(Wrappers.lambdaQuery(Patient.class)
                    .eq(Patient::getPatientId, pc.getPatientId()));
            if (null!=patient){
                exit = false;
            }
        }
        if (exit){
            // 添加就诊人信息
            Patient patient = new Patient();
            patient.setAccountId(UserUtils.getLoginUser());
            BeanUtil.copyProperties(patientDto,patient);
            patientMapper.insert(patient);
            //添加就诊人证件信息
            PatientCertificate patientCertificate = new PatientCertificate();
            patientCertificate.setVerified(patientDto.getVerified());
            patientCertificate.setPatientId(patient.getPatientId());
            patientCertificate.setCertNo(patientDto.getCertNo());
            patientCertificate.setCertType(CertType.getTypeInt(patientDto.getCertType()));
            patientCertificateService.save(patientCertificate);
            return true;
        }else {
            Asserts.fail("该就诊人已经存在");
            return false;
        }
    }

    @Override
    public PatientVo findByPatientId(String patientId) {
        PatientVo patientVo = new PatientVo();
        Patient patient = patientMapper.selectById(patientId);
        if (null!=patient){
            BeanUtil.copyProperties(patient,patientVo);
            System.out.println(patient.getCreateTime());
            patientVo.setName(DesensitizationUtils.desensitizedName(patient.getName()));
            patientVo.setPhoneNumber(DesensitizationUtils.desensitizedPhoneNumber(patient.getPhoneNumber()));
            PatientCertificate patientCertificate = patientCertificateMapper.selectOne(
                    Wrappers.lambdaQuery(PatientCertificate.class).eq(PatientCertificate::getPatientId, patientId));
            if (null!=patientCertificate){
                patientVo.setCertNo(DesensitizationUtils.desensitizedIdNumber(patientCertificate.getCertNo()));
                patientVo.setCertType(CertType.getTypeStr(patientCertificate.getCertType()));
                //根据身份证计算年龄
                if (0==patientCertificate.getCertType()){
                    Integer age = IDCardUtil.getAge(patientCertificate.getCertNo());
                    patientVo.setAge(age);
                }
            }
        }
        return patientVo;
    }

    @Override
    public List<PatientListVo> listByAccountId() {
        return patientMapper.listByAccountId(UserUtils.getLoginUser());
    }

    @Override
    public boolean updatePatient(PatientDto patientDto) {
        boolean exit = true;
        PatientCertificate pc = patientCertificateMapper.selectOne(Wrappers.lambdaQuery(PatientCertificate.class)
                .eq(PatientCertificate::getCertNo, patientDto.getCertNo()));
        if (null!=pc){
            if (pc.getPatientId()!=patientDto.getPatientId()){
                exit = false;
            }
        }

        if (exit){
            //添加就诊人信息
            Patient patient = new Patient();
            patient.setAccountId(UserUtils.getLoginUser());
            BeanUtil.copyProperties(patientDto,patient);
            patientMapper.updateById(patient);
            //添加就诊人证件信息
            PatientCertificate patientCertificate = new PatientCertificate();
            patientCertificate.setVerified(patientDto.getVerified());
            patientCertificate.setCertNo(patientDto.getCertNo());
            patientCertificate.setCertType(CertType.getTypeInt(patientDto.getCertType()));
            patientCertificateService.update(patientCertificate,Wrappers.lambdaQuery(PatientCertificate.class)
                    .eq(PatientCertificate::getPatientId,patientCertificate.getPatientId()));
            return true;
        }else{
            Asserts.fail("该证件号码已经存在");
            return false;
        }
    }

}
